package com.ocom.wxapi.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.dto.CompanyInfoDto;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.entity.master.EntityMngUserInfo;
import com.ocom.common.entity.master.EntityWechatPerInfo;
import com.ocom.common.pojo.company.CompanyListPojo;
import com.ocom.common.request.wx.RegisterPageBindRequest;
import com.ocom.common.request.wx.RegisterPageRegisterRequest;
import com.ocom.common.request.wx.UnBindRequest;
import com.ocom.common.request.wx.UserPageAddUserRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.wxapi.client.CompanyClientService;
import com.ocom.wxapi.client.HumanClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@PreAuthorize("hasRole('ROLE_WX')")
@RequestMapping("/RegisterPage")
@RestController
public class RegisterPageController {

    @Value("${wx.appid}")
    String appId;

    @Autowired
    HumanClientService humanClientService;

    @Autowired
    CompanyClientService companyClientService;


    @PostMapping("/onShow")
    public Result add(@Valid @NotBlank String openid)  {  //@Valid TestRequest request

        Map<String,String> map=new HashMap<>();
        Map<String,Object> data=new HashMap<>();

        map.put("pageTitle","OCOM云卡通+");
        map.put("lblNewCompany","新单位名称");
        map.put("lblColor","选择肤色");
        map.put("cmdNew","注册新单位");
        map.put("cmdBind","绑定单位");
        data.put("pageFormat",map);

        Result result=companyClientService.getCmpanyListByOpenid(openid);
        if(result.getCode()==200){
            List<CompanyListPojo> list=JSON.parseObject(JSON.toJSONString(result.getData()), List.class);
            data.put("companyList",list);
        }



        return ResultUtil.success(data);
    }


    @PostMapping("/register")
    public Result register(@Valid RegisterPageRegisterRequest request){
        //1传输appid
        request.setAppId(appId);

        CompanyInfoDto companyInfoDto=new CompanyInfoDto();
        companyInfoDto.setComName(request.getCompanyName());
        companyInfoDto.setColor(request.getColorNo());
        companyInfoDto.setCreateUserid(1L);

        Result result=companyClientService.AddCompanyInfo(companyInfoDto);
        if(result.getCode()!=200){
            return ResultUtil.error(result.getCode());
        }
        Map<String,Object> map=JSON.parseObject(JSON.toJSONString(result.getData()), HashMap.class);
        Long companyId=Long.valueOf(String.valueOf(map.get("comId")));

        //2添加管理员
        EntityMngUserInfo entityMngUserInfo=new EntityMngUserInfo();
        entityMngUserInfo.setComId(companyId);
        entityMngUserInfo.setDeptId(0L);
        entityMngUserInfo.setUserName(request.getCompanyName());
        entityMngUserInfo.setPassword( "123456");
        entityMngUserInfo.setRole( 21L);
        companyClientService.AddCompanyAdminInfo(entityMngUserInfo);

        //3分表添加用户
        UserPageAddUserRequest userPageAddUserRequest=new UserPageAddUserRequest();
        userPageAddUserRequest.setCompanyId(companyId);
        userPageAddUserRequest.setUserName(request.getCompanyName());
        userPageAddUserRequest.setDeptId(0L);
//        userPageAddUserRequest.setTelephone("123456");
//        userPageAddUserRequest.setIdentity("123456");
        Result result1=humanClientService.addPerUser(userPageAddUserRequest);
        EntityPersonnelInfo entityPersonnelInfo =JSON.parseObject(JSON.toJSONString(result1.getData()), EntityPersonnelInfo.class);

        //4.添加微信绑定用户 wechat_per_info
        EntityWechatPerInfo wechatPerInfo=new EntityWechatPerInfo();
        wechatPerInfo.setOpenid(request.getOpenid());
        wechatPerInfo.setComId(companyId);
        if(entityPersonnelInfo!=null){
            wechatPerInfo.setPId(entityPersonnelInfo.getPId());

            //更新单位创建人
            companyClientService.modifyPid(companyId,entityPersonnelInfo.getPId());
        }
        humanClientService.addWxper(wechatPerInfo);

        Map<String,Object> map1=new HashMap<>();
        map1.put("companyId",companyId);
        map1.put("pId",0L);
        if(entityPersonnelInfo!=null){
            map1.put("pId",entityPersonnelInfo.getPId());
        }
        map1.put("pRoles",21);
        return ResultUtil.success(map1);
    }


    @PostMapping("/bind")
    public Result bind(@Valid RegisterPageBindRequest request){

        //传输appid
        request.setAppId(appId);

        //1.判断企业名称  密码
        Result result=companyClientService.isExistCompany(request.getUserName(),request.getPwd());
        
        if(result.getCode()!=200){
            return ResultUtil.info(result.getCode(),result.getMsg());
        }
        Map<String,Integer> map = JSON.parseObject(JSON.toJSONString(result.getData()), HashMap.class);
        //绑定 添加微信用户

        //2. 绑定用户
        EntityWechatPerInfo entityWechatPerInfo=new EntityWechatPerInfo();
        entityWechatPerInfo.setComId(Long.valueOf(map.get("comId")));
        entityWechatPerInfo.setPId(Long.valueOf(map.get("pId")));
        entityWechatPerInfo.setOpenid(request.getOpenid());
        entityWechatPerInfo.setCreateTime(new Date());
        entityWechatPerInfo.setAppid(appId);

        humanClientService.addWxper(entityWechatPerInfo);

        Map<String,Integer> returnMap=new HashMap<>();
        returnMap.put("companyId",map.get("comId"));
        returnMap.put("pId",map.get("pId"));
        returnMap.put("pRoles",21);

        return ResultUtil.success(returnMap);
    }


    @PostMapping("/unBind")
    public Result unBind(@Valid  UnBindRequest request) {
        request.setAppid(appId);
        request.setOpenid(SecurityUtils.getUsername());
        companyClientService.unbingWxCompanyAdmin(request);
        return ResultUtil.success();
    }

}
